import { defineStore } from 'pinia';
import Cookies from "js-cookie";
import { ref } from "vue";

export const useStore = defineStore('user', () => {
  // 用户令牌
  const token = ref('');
  //用户id
  const uid = ref('');
  // 用户信息
  const user_info = ref({} as any);

  const setToken = (e: string) => {
    token.value = e || '';
  }
  const setUid = (e: string) => {
    uid.value = e || '';
  }
  const setUserInfo = (e: any) => {
    user_info.value = e || {};
  }
  const logout = () => {
    token.value = '';
    uid.value = '';
    user_info.value = {};
  }

  return {
    token,
    uid,
    user_info,
    setToken,
    setUid,
    setUserInfo,
    logout,
  }
}, {
  persist: {
    enabled: true,
    strategies: [{
      key: "user",
      storage: {
        getItem: (key: string) => Cookies.get(key),
        setItem: (key: string, val: any) => Cookies.set(key, val),
        removeItem: (key: string) => Cookies.remove(key),
      },
    }]
  }
} as any)